﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLNSClient
{
    public partial class Frm_PCGV : DevExpress.XtraEditors.XtraForm
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public Frm_PCGV()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            txt_MaPCGV.Enabled = false;
            txt_TenPCGV.Enabled=false;
            txt_HeSoPCGV.Enabled=false;

            gv_dsPCGV.DataSource = sv.getPCGV();
            gridView1.Columns[0].Caption = "Mã PCGV";
            gridView1.Columns[1].Caption = "Tên PCGV";
            gridView1.Columns[2].Caption = "Hệ số PCGV (%)";
        }

        private void Frm_PCGV_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            List<QLNSServiceReference.PHUCAP_GIAOVIEN> listLastPCGV = new List<QLNSServiceReference.PHUCAP_GIAOVIEN>(sv.getLastPCGV());
            int lastID = listLastPCGV[0].Ma_PCGV;

            txt_MaPCGV.Text = (lastID + 1).ToString();
            
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            txt_MaPCGV.Enabled = false;
            txt_TenPCGV.Enabled = true;
            txt_HeSoPCGV.Enabled = true;

            txt_TenPCGV.Text = "";
            txt_HeSoPCGV.Text = "";

        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            string tempMaPCGV = txt_MaPCGV.Text;
            string tempTenPCGV = txt_TenPCGV.Text;
            string tempHeSoPCGV;
            if (txt_HeSoPCGV.Text == "")
            {
                tempHeSoPCGV = null;
            }
            else
            {
                tempHeSoPCGV = txt_HeSoPCGV.Text;
            }

            QLNSServiceReference.PHUCAP_GIAOVIEN pcgv = new QLNSServiceReference.PHUCAP_GIAOVIEN();
            pcgv.Ma_PCGV = Int32.Parse(tempMaPCGV);
            pcgv.TenPCGV = tempTenPCGV;
            if (tempHeSoPCGV == null)
            {
                pcgv.HeSoPCGV = 0;
            }
            else
            {
                pcgv.HeSoPCGV = Int32.Parse(tempHeSoPCGV);
            }

            if (demsua == 1)
            {
                DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật lại thông tin?!", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sv.updatePCGV(pcgv);
                    LoadData();
                    demsua = 0;
                    MessageBox.Show("Cập nhật thông tin phụ cấp giáo viên mã '"+txt_MaPCGV.Text+"' thành công!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                    int kq=sv.addPCGV(pcgv);
                    if (kq != -1)
                    {
                        LoadData();
                        MessageBox.Show("Thêm phụ cấp giáo viên mã '"+txt_MaPCGV.Text+"' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
            }

        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;

            txt_MaPCGV.Enabled = false;
            txt_TenPCGV.Enabled = false;
            txt_HeSoPCGV.Enabled = false;

            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            demsua = 1;

            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;

            txt_TenPCGV.Enabled = true;
            txt_HeSoPCGV.Enabled = true;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
        }

        private void gv_dsPCGV_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            txt_MaPCGV.Enabled = false;
            txt_TenPCGV.Enabled = false;
            txt_HeSoPCGV.Enabled = false;
            try
            {
                txt_MaPCGV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_TenPCGV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                txt_HeSoPCGV.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
            }
            catch { }
        }
    }
}